// user/userinfo/userinfo.js
Page({
  data: {
    tempFilePaths: [],
    name:"",
    emall:'',
    phone:'',
    TFname:false,
    TFemall:false,
    TFphone:false
  },
  getname(e){
    var name = e.detail.value
    if(name.length >= 20){
      this.setData({
        TFname:true
      })
    }
    else{
      this.setData({
        TFname:false
      })
    }
    this.setData({
      name:name
    })
  },
  getemall(e){
    console.log(e.detail.value)
    var emall  = e.detail.value
    var emalls  = e.detail.value
    var qq = "@qq.com"
    var wy = "@163.com"
    var lens = emall.length
    if((lens > 7 && (emall.substr(lens-7) === qq|| emall.substr(lens-8) === wy ))|| lens === 0 ){
      this.setData({
        TFemall:false
      })
    }
    else{
      this.setData({
        TFemall:true
      })
    }
    this.setData({
      emall:emalls
    })
  },
  getphone(e){
    var phone = e.detail.value
    if( !(/^\d{11}$/.test(phone)))
    {
      this.setData({
        TFphone:true
      })
    }
    else{
      this.setData({
        TFphone:false
      })
    }
    this.setData({
      phone:phone
    })
  },
  img_item(e) {
    var that = this;
    wx.chooseMedia({
      count: 1,
      mediaType: ['image'],
      sourceType: ['album'],
      maxDuration: 30,
      camera: 'back',
      success(res) {
        // console.log(res.tempFiles)
        that.setData({
          tempFilePaths:res.tempFiles
        })
      }
    })
  },

  //POST
  formSubmit(e) {
    var that = this
    var ans = !that.data.TFemall & !that.data.TFname & !that.data.TFphone
    var phone = this.data.phone
    if (ans && that.data.tempFilePaths.length==1 && phone.length == 11) {
        that.btn_up()
        console.log("123s4")
      } else {
        wx.showToast({
          title: '请按要求填写',
          icon: 'error',
          duration: 1500
        })
      }
    
  },
  btn_up() {
    var that = this;
    var name = that.data.name
    var emall = that.data.emall
    var phone = that.data.phone
    console.log(that.data.tempFilePaths[0])
    var filePath = that.data.tempFilePaths[0].tempFilePath
    wx.uploadFile({
      url: getApp().globalData.urlpath + "/weixin/userupdate",
      filePath: filePath,
      name: 'image',
      timeout: 600000,
      formData: {"uid":getApp().globalData.uid,"name":name,"eamll":emall,"phone":phone},
      success: function (res) {
        console.log(res)
        let uid = getApp().globalData.uid
        let power = getApp().globalData.power
        that.setData({
          tempFilePaths:[],
          name:"",
          emall:"",
          phone:""
        })
        if(res.data.ret === "F1")
        {
          wx.showToast({
            title: '图片存在问题',
            icon: 'error',
            duration: 1500
          })
        }
        else if(res.data.ret === "F2")
        {
          let photo = res.data.photo
          let name = getApp().globalData.name
          getApp().globalData.photo = photo
          console.log(getApp().globalData.photo)
          wx.setStorageSync('user_Datas', {"uid":uid,"power":power,"photo":photo,"name":name })
        }
        else if(res.data.ret === "F3"){
          let photo = res.data.photo
          let name = res.data.name
          getApp().globalData.photo = photo
          getApp().globalData.photo = name
          try {
            wx.setStorageSync('user_Datas', {"uid":uid,"power":power,"photo":photo,"name":name })
          } catch (error) {
          }
         
        }
      },
      fail(res){
        console.log(res)

      }
    })
  }
})